package com.joshua.spring.controller;

import com.joshua.spring.entity.vo.GlobalResponse;
import com.joshua.spring.form.ContainsArrayObjForm;
import com.joshua.spring.form.ParamsObjFrom;
import com.joshua.spring.spi.FtConsumerClient;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

import static com.joshua.spring.constant.ProviderApiConstants.Demo.*;
import static com.joshua.spring.constant.ProviderApiConstants.*;

/**
 * description
 * package com.spring.joshua.controller
 * classname  ProviderDemoController
 *
 * @author <a href="mailto:joshualwork@163.com">joshua_liu</a>
 * @date 2020/3/30 16:30
 */
@Api(tags = "服务提供者demo")
@Slf4j
@RestController
@RequestMapping(PROVIDER)
public class ProviderDemoController {
    @Autowired
    private RedisTemplate redisTemplate;

    private static final String PATH_ID = Demo.PATH_ID;

    @Autowired
    private FtConsumerClient consumerClient;

    @GetMapping
    @ApiOperation(value = "获取生产者信息")
    public GlobalResponse paramsObj(@ModelAttribute ParamsObjFrom form) {
        form.setName(PROVIDER + form.getName());
        form.setAge(PROVIDER + form.getAge());
        form.setTotal(PROVIDER + form.getTotal());
        return GlobalResponse.buildSuccess(form);
    }

    @GetMapping(value = {PATH_ID, "id/{id}"})
    public GlobalResponse pathInfo(@PathVariable String id) {
        log.info("Path id test");
        return GlobalResponse.buildSuccess(PROVIDER + id);
    }

    /**
     * description: {@link com.joshua.spring.spi.ProviderDemoClient#arrayInfo(String[])}
     *
     * @param ids @RequestParam(value = "ids")如果是通过feign暴露接口
     *            接口本身和feignClient中必须声明value,否则数组中的值
     *            无法传输
     * @return {@link GlobalResponse}
     * @author <a href="mailto:joshualwork@163.com">joshua_liu</a>
     * @date 2020/9/1 15:18
     */
    @GetMapping(ID_ARRAY)
    public GlobalResponse arrayInfo(@RequestParam(value = "ids") String[] ids) {
        return GlobalResponse.buildSuccess(ids);
    }

    /**
     * description: {@link com.joshua.spring.spi.ProviderDemoClient#listInfo(ContainsArrayObjForm)}
     *
     * @param form 如果是通过feign暴露接口，form对象中不要有 数组，否则会导致数组传递失败
     *             可以选择把数组用@RequestParam单独传过来
     * @return {@link GlobalResponse}
     * @author <a href="mailto:joshualwork@163.com">joshua_liu</a>
     * @date 2020/9/1 15:22
     */
    @GetMapping(QUERY_LIST)
    public GlobalResponse listInfo(@ModelAttribute ContainsArrayObjForm form) {
        List<String> arrayInfos = Arrays.asList(form.getArray());
        log.info("ArrayInfos====>[{}]", arrayInfos);
        return GlobalResponse.buildSuccess(form);
    }

    @GetMapping(QUERY_REMOTE)
    public GlobalResponse test() {
        return consumerClient.pathInfo();
    }
}
